package com.weihui.jxc.view.operater;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.weihui.jxc.common.CommonFactory;
import com.weihui.jxc.model.Customer;
import com.weihui.jxc.model.Item;
import com.weihui.jxc.model.Operator;
import com.weihui.jxc.service.OperatorService;
import com.weihui.jxc.view.LoginFrame;

import javax.swing.JTextField;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

/**
 * 操作员编辑
 * 
 * @author FPF
 *
 */
public class OperatorEditPanel extends JPanel implements ActionListener{
	private JTextField nameTf;
	JComboBox comboBox_1;
	JComboBox comboBox;
	private OperatorService operatorService;

	/**
	 * Create the panel.
	 */
	public OperatorEditPanel() {

		JPanel panel = new JPanel();

		JPanel panel_1 = new JPanel();

		JPanel panel_2 = new JPanel();

		JPanel panel_3 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addGroup(groupLayout
				.createSequentialGroup().addContainerGap()
				.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE))
				.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addContainerGap()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(108, Short.MAX_VALUE)));

		JButton btnNewButton = new JButton("修改");
		btnNewButton.addActionListener(this);
		panel_3.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("删除");
		btnNewButton_1.addActionListener(this);
		panel_3.add(btnNewButton_1);

		JLabel lblNewLabel_2 = new JLabel("操作员权限：");
		panel_2.add(lblNewLabel_2);

		comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] { "超级管理员", "普通管理员", "仓库人员","销售人员","采购人员" }));

		panel_2.add(comboBox_1);

		JLabel lblNewLabel_1 = new JLabel("操作员姓名：");
		panel_1.add(lblNewLabel_1);

		nameTf = new JTextField();
		panel_1.add(nameTf);
		nameTf.setColumns(20);

		JLabel lblNewLabel = new JLabel("选择操作员：");
		panel.add(lblNewLabel);

		comboBox = new JComboBox();
		// 给comboBox设置选中事件
		comboBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				selectAction();
			}

		});
		panel.add(comboBox);
		setLayout(groupLayout);
		operatorService = CommonFactory.getOperatorService();
		initComboBox();
	}

	/**
	 * 初始化操作者下拉选择框
	 */
	public void initComboBox() {
		// 获取数据库中的数据
		List<Operator> list = operatorService.queryAll(null);
		// 设置下来列表框中数据模型
		Vector<Item> items = new Vector<>();
		// 清空下拉列表框中的内容，免的重复
		comboBox.removeAllItems();
		// 迭代处理数据
		for (int i = 0; i < list.size(); i++) {
			Item item = new Item();
			item.setId(list.get(i).getUsername());
			item.setName(list.get(i).getName());
			if (items.contains(item)) {// 如果集合中包含该元素，就不在添加
				continue;
			}
			items.add(item);
			comboBox.addItem(item);
		}
	}

	/**
	 * 下拉列表框点击事件
	 */
	private void selectAction() {

		if (!(comboBox.getSelectedItem() instanceof Item)) {
			return;
		}
		Item selectedItem = (Item) comboBox.getSelectedItem();
		// 根据登录名查询数据库
		Operator o = operatorService.getOperator(selectedItem.getId());
		nameTf.setText(o.getName());
		switch (o.getPower()) {
		case "超级管理员":
            comboBox_1.setSelectedIndex(0);
			break;
		case "普通管理员":
			comboBox_1.setSelectedIndex(1);
			break;
		case "员工":
			comboBox_1.setSelectedIndex(2);
			break;
		default:
			break;
		}

	}
    /**
     * 点击事件
     */
	@Override
	public void actionPerformed(ActionEvent e) {
		String target = e.getActionCommand();
		switch (target) {
		case "修改":
			if(nameTf.getText().trim()==null || nameTf.getText().length()==0) {
				JOptionPane.showMessageDialog(null, "真实姓名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			Operator o = new Operator();
			o.setUsername(((Item)(comboBox.getSelectedItem())).getId());
			o.setName(nameTf.getText().trim());
			o.setPower(comboBox_1.getSelectedItem().toString());
			boolean isFlag = operatorService.updateOperator(o);
			if(isFlag==true) {
				JOptionPane.showMessageDialog(null, "操作员修改成功！","提示",JOptionPane.INFORMATION_MESSAGE);
				initComboBox();
			}else {
				JOptionPane.showMessageDialog(null, "操作员修改失败！","警告",JOptionPane.WARNING_MESSAGE);
			}
			break;
		case "删除":
			int result = JOptionPane.showConfirmDialog(null, "是否要删除所选的操作员！","警告",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
			if(result==0) {
				String username =((Item)(comboBox.getSelectedItem())).getId();
				if(username.equals(LoginFrame.getLoginedUser().getUsername())) {
					JOptionPane.showMessageDialog(null, "不能删除当前登录的用户！","提示",JOptionPane.INFORMATION_MESSAGE);		
					return;
				}
				boolean flag = operatorService.deleteOperator(username);
				if(flag==true) {
					JOptionPane.showMessageDialog(null, "操作员删除成功！","提示",JOptionPane.INFORMATION_MESSAGE);	
					initComboBox();
				}else {
					JOptionPane.showMessageDialog(null, "操作员删除失败！","警告",JOptionPane.WARNING_MESSAGE);
				}
			}
			break;
		default:
			break;
		}
		
	}

}